package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://hsrb.com.cn/
 * @author Administrator
 * abc
 */
public class HsrbNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://hsrb.com.cn/a/news/.*/[0-9]{4}/[0-9]{4}/[0-9]*.html",
		     };
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);
		title=doc.select("h2").text();
		
		content=HtmlCleaner.getContentHtml(url,doc.select("td.a14-25"));
		
		date=DateUtils.matchDate(doc.select("div.info").text());
		if(date == null){
			date=DateUtils.matchDate(url);
		}
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			return news;

	}
	public static void main(String[] args) {
		HsrbNewsAnalyse hsw=new HsrbNewsAnalyse();
		String url="http://hsrb.com.cn/a/news/huchengyaowen/2015/0626/115165.html";
//		String url="http://it.hsw.cn/071378/346676513197b.shtml";
//		String url="http://finance.hsw.cn/system/2012/11/22/051541613.shtml";
		System.out.println(hsw.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);

		NewsMeta parserHtml=hsw.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
